package com.atguigu.gmall.user.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.service.UserInfoService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


//api/user/passport/login
@RequestMapping("/api/user")
@RestController
public class UserController {


    @Autowired
    UserInfoService userInfoService;



    /**
     * 用户登录
     * @param userInfo
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("/passport/login")
    public Result userLogin(@RequestBody UserInfo userInfo) throws JsonProcessingException {

        Map<String, Object> login = userInfoService.login(userInfo);

        //前端希望有一个data:  部分有一个token
        //前端希望有一个data:  data也是一个对象。id（用户id） nickName
        if(login == null){
            //登录失败
            return Result.build(null, ResultCodeEnum.USERNAME_PASSWORD_ERROR);
        }


        return Result.ok(login);
    }


    @GetMapping("/passport/logout")
    public Result logout(@RequestHeader("token") String token){
        userInfoService.logout(token);
        return Result.ok();
    }


}
